package com.cjh.db2doc;

import com.cjh.db2doc.conn.DataSourceConfig;
import com.cjh.db2doc.table.TableColumn;
import com.cjh.db2doc.table.TableInfo;
import com.zaxxer.hikari.HikariDataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * com.cjh.db2doc 2022/7/26 10:25
 *
 * @author chenjh
 */
public class TableMetadata {

    public static List<TableInfo> getTableList(GeneratorConfig generatorConfig)
            throws SQLException {
        DataSourceConfig dataSourceConfig = generatorConfig.getDataSourceConfig();
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setPassword(dataSourceConfig.getPassword());
        dataSource.setUsername(dataSourceConfig.getUsername());
        dataSource.setJdbcUrl(dataSourceConfig.getJdbcUrl());
        dataSource.setDriverClassName(dataSourceConfig.getDriverClassName());
        //
        List<TableInfo> list = new ArrayList<>();
        //
        try (Connection connection = dataSource.getConnection()) {
            ResultSet resultSet = connection.getMetaData()
                    .getTables(dataSourceConfig.getDbname(), null, null, new String[]{"TABLE"});
            while (resultSet.next()) {
                String TABLE_CAT = resultSet.getString(1);
                String TABLE_SCHEM = resultSet.getString(2);
                String TABLE_NAME = resultSet.getString(3);
                String TABLE_TYPE = resultSet.getString(4);
                String REMARKS = resultSet.getString(5);
                String TYPE_CAT = resultSet.getString(6);
                String TYPE_SCHEM = resultSet.getString(7);
                String TYPE_NAME = resultSet.getString(8);
                String SELF_REFERENCING_COL_NAME = resultSet.getString(9);
                String REF_GENERATION = resultSet.getString(10);
//                System.out.println(TABLE_CAT);
//                System.out.println(TABLE_SCHEM);
//                System.out.println(TABLE_NAME);
//                System.out.println(TABLE_TYPE);
//                System.out.println(REMARKS);
//                System.out.println(TYPE_CAT);
//                System.out.println(TYPE_SCHEM);
//                System.out.println(TYPE_NAME);
//                System.out.println(SELF_REFERENCING_COL_NAME);
//                System.out.println(REF_GENERATION);
//                System.out.println("-------------------------------");
                List<TableColumn> columnList = getColumnInfo(connection,
                        dataSourceConfig.getDbname(), TABLE_NAME);
                TableInfo tableInfo = new TableInfo();
                tableInfo.setTableName(TABLE_NAME);
                tableInfo.setRemark(REMARKS);
                tableInfo.setTableColumnList(columnList);
                list.add(tableInfo);
            }
            return list;
        } catch (SQLException e) {
            throw e;
        }
    }

    public static List<TableColumn> getColumnInfo(Connection connection, String dbName,
            String tableName)
            throws SQLException {
        ResultSet columns = connection.getMetaData()
                .getColumns(dbName, null, tableName, "%");
        int columnLength = columns.getMetaData().getColumnCount();
//        for (int i = 1; i <= columnLength; i++) {
//            String columnName = columns.getMetaData().getColumnName(i);
//            System.out.print(columnName);
//            System.out.print('\t');
//        }
        List<TableColumn> list = new ArrayList<>();
        int i = 1;
        while (columns.next()) {
            String column_name = columns.getString("COLUMN_NAME");
            String type_name = columns.getString("TYPE_NAME");
            String column_size = columns.getString("COLUMN_SIZE");
            String remarks = columns.getString("REMARKS");
            TableColumn tableColumn = new TableColumn();
            tableColumn.setColumnNo(i++);
            tableColumn.setColumnName(column_name);
            tableColumn.setColumnType(type_name);
            tableColumn.setColumnLength(column_size);
            tableColumn.setColumnRemark(remarks);
            list.add(tableColumn);
        }
        return list;
    }

}
